/*
 * SRMI (an easy-to-use zero-code remoting library for Java)
 * Copyright (C) 2007  Mohammad Nabil Hussein
 * mohammad (dot) nabil (dot) h (at) gmail (dot) com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package SRMI.Tests;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.lang.instrument.ClassDefinition;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.InstructionFactory;
import org.apache.bcel.verifier.VerificationResult;
import org.apache.bcel.verifier.Verifier;
import org.apache.bcel.verifier.VerifierFactory;

public abstract class BasicInstrumenter {
    JavaClass jclas;
    ClassGen cgen;
    InstructionFactory _factory;

    public BasicInstrumenter(JavaClass clazz) {
        jclas = clazz;
        cgen = new ClassGen(jclas);
        _factory = new InstructionFactory(cgen);
    }
    
    public BasicInstrumenter(String path) throws IOException
    {
        this(new ClassParser(path).parse());
    }
    
    public BasicInstrumenter(byte[] clazz, String name) throws IOException {        
        this(new ClassParser(new DataInputStream(new ByteArrayInputStream(clazz)), name).parse());
    }
    
    public BasicInstrumenter(Class<?> clazz) throws IOException
    {
        this(Agent.cache.get(clazz.getCanonicalName().replaceAll("\\.", "/")),clazz.getName());                
    }

    
    public void verify(String qualifiedClassName) {
        if(!verify)
            return;
        Verifier v = VerifierFactory.getVerifier(qualifiedClassName);
        VerificationResult vr = v.doPass1();
        if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
            System.out.println("Pass 1:\n" + vr);
            System.exit(1);
        }
        vr = v.doPass2();
        if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
            System.out.println("Pass 2:\n" + vr);
            System.exit(1);
        }
        for (int i = 0; i < jclas.getMethods().length; i++) {
            vr = v.doPass3a(i);
            if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
                System.out.println("Pass 3a, method number " + i + " ['" + jclas.getMethods()[i] + "']:\n" + vr);
                System.exit(1);
            }
            vr = v.doPass3b(i);
            if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
                System.out.println("Pass 3b, method number " + i + " ['" + jclas.getMethods()[i] + "']:\n" + vr);
                System.exit(1);
            }
        }
    }
    
    public abstract byte[] instrument() throws Exception;
    
    public final static boolean verify = true;    
        
    public static void useInstrumenter(Class<?> clazz, Class<? extends BasicInstrumenter> instrumenter) throws Exception
    {        
        BasicInstrumenter instrumenterObj = (BasicInstrumenter) instrumenter.getConstructor(Class.class).newInstance(clazz);
        ClassDefinition classDef = new ClassDefinition(clazz, instrumenterObj.instrument());
        Agent.redefineClasses(classDef);
    }
} 
